import {
	ListActionType,
	ICtrlDataAction,
	ICtrlEvent,
	IListAbility,
	IListControllerParams,
	IListModel,
	IListStore,
	IParam,
	IListController,
} from '@/core';
import { QXEvent } from 'qx-util';
import { PropType } from 'vue';
import {
	getCtrlClassNames,
	handleCtrlAction,
	handleCtrlDestroy,
	handleCtrlInit,
	useCtrlAction,
	useCtrlBasic,
	useCtrlControllerParams,
	useCtrlProps,
} from '../use-ctrl';

/**
 * 列表输入参数
 *
 * @export
 * @return {*}
 */
export function useListProps() {
	return {
		...useCtrlProps(),
		openView: Function,
		newView: Function,
		actions: Object as PropType<ICtrlDataAction>,
		selectFirstDefault: Boolean,
		singleSelect: {
			type: Boolean,
			default: false,
		},
		rowActiveMode: {
			type: Number as PropType<0 | 1 | 2>,
			default: 2,
		},
	};
}

/**
 * 列表基础参数
 *
 * @export
 * @param {IListModel} model
 * @param {IParam} props
 * @return {*}
 */
export function useListBasic(model: IListModel, props: IParam) {
	const getClassNames = (props: IParam) => {
		const names = getCtrlClassNames(model, props);
		return names;
	};
	return {
		...useCtrlBasic(model, props),
		getClassNames,
		handleCtrlInit,
		handleCtrlAction,
		handleCtrlDestroy,
	};
}

/**
 * 列表部件行为
 *
 * @export
 */
export function useListAction(
	controller: IListController<IListStore, IListAbility>
) {
	const handleItemClick = (row: IParam, event: MouseEvent) => {
		controller.handleItemClick(row, event);
	};

	const handleItemDbClick = (row: IParam, event: MouseEvent) => {
		controller.handleItemDbClick(row, event);
	};

	const handleSelectionChange = (selections: IParam[]) => {
		controller.handleSelectionChange(selections);
	};

	const handleLoadMore = () => {
		controller.loadMore();
	};
	return {
		...useCtrlAction(controller),
		handleItemClick,
		handleItemDbClick,
		handleSelectionChange,
		handleLoadMore,
	};
}

/**
 * 列表部件控制器
 *
 * @export
 */
export function useListControllerParams<
	S extends IListStore,
	M extends IListModel
>(
	model: M,
	props: any,
	evt: QXEvent<ICtrlEvent<ListActionType, IListAbility>>,
	otherParams: IParam = {}
): IListControllerParams<ListActionType, IListAbility> {
	return {
		...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
		actions: props.actions,
		rowActiveMode: props.rowActiveMode,
		selectFirstDefault: props.selectFirstDefault,
		singleSelect: props.singleSelect,
		openView: props.openView,
		newView: props.newView,
	};
}
